<template>
  <div class="container" ref="mainCon">
    <!-- 标题 -->
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <!-- 搜索 -->
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                placeholder="请选择"
                :props="{ checkStrictly: true }"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item
              label="开户银行"
              class="row-padding-bottom"
              label-width="135px"
            >
              <el-select
                v-model="filterField.bankId"
                placeholder="请选择"
                clearable
                filterable
              >
                <el-option
                  v-for="(item, index) of bankData"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="专户账号" class="row-padding-bottom">
                <el-input
                  placeholder="请输入专户账号"
                  clearable
                  v-model="filterField.account"
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item>
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                class="searchBtn"
                @click="searchList"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <el-button
            class="commonBut saveBtn funComBut"
            icon="icon iconfont icontianjia"
            @click="toAdd"
          >
            <span class="opetareText">添加</span>
          </el-button>
        </div>
        <!-- 表格 -->
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="areaName"
            label="区域"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="bankName"
            label="开户银行"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="bankSiteName"
            label="银行网点"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="account"
            label="专户账号"
            align="center"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.account | deseHandler(4, 4, '*') || '' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="expensesTotal"
            sortable="custom"
            label="总支出（元）"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="balance"
            sortable="custom"
            label="余额（元）"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column label="操作" align="left">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="seeDetail(scope.row)"
                >查看</el-link
              >
              <el-link
                type="primary"
                v-if="isShowUpdate"
                @click="toUpdate(scope.row)"
                >修改</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- 分页 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
      <addAccountDialog
        v-if="accountFormVisible"
        :accountFormVisible="accountFormVisible"
        :operationType="operationType"
        @close-dialogStatus="close_addDialog"
        :accountInfo="accountInfo"
        :isDisabled="isDisabled"
      ></addAccountDialog>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'
import addAccountDialog from './addAccountDialog'
export default {
  name: 'specialAccountManage',
  mixins: [scrollTable],
  components: {
    addAccountDialog,
  },
  data() {
    return {
      advanced: false,
      titleText: '企业专户管理', //企业端
      titleExplain: '用于管理该企业专户信息，详情页面可查看对应专户流水数据',
      requestUrl: 'api-acc/companyaccount/expand/companyAccountList',
      //表格数据
      tableList: [],
      isShow: false, //列表加载
      loading: false,
      filterField: {
        areaId: '',
        bankId: '',
        account: '',
      },
      pageSize: 20,
      pageNo: 1,
      areaId: '',
      bankData: [],
      accountFormVisible: false, //添加专户dialog
      operationType: '', //专户添加/修改操作
      accountInfo: {}, //传递子组件专户信息
      isShowUpdate: '',
      isShowDetail: '',
      isDisabled: false,
    }
  },
  computed: { ...mapState(['areasOptions']) },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.getBankList()
    this.isShowUpdate =
      this.$store.state.buttonLimitsArr.indexOf('ycCompanyAccount:update') > -1
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('ycCompanyAccount:detail') > -1
  },
  methods: {
    ...mapActions(['getAreas']),
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //查看详情
    seeDetail(row) {
      this.$router.push({
        path: '/home/yiChangCustomRequirements/enterpriseFlowManagement',
        name: 'enterpriseFlowManagement',
        query: {
          account: row.account,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/yiChangCustomRequirements/specialAccountManage',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toAdd() {
      this.axios({
        method: 'post',
        url: 'api-acc/companyaccount/expand/addcompanyaccountfindconfig',
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.accountInfo.account = ''
            this.operationType = 'addAccount'
            this.accountFormVisible = true
          } else {
            this.$alert(res.data.errorMsg, '提示', {
              confirmButtonText: '知道了',
            })
          }
        })
        .catch((error) => {
          this.$message({
            message: '请求失败',
            type: 'error',
          })
        })
    },
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getBankList() {
      this.axios({
        method: 'post',
        data: { type: 'BANK_TYPE' },
        url: 'api-bs/base/dicitem/findbytype',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.bankData = response.data.result
            } else {
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    toUpdate(row) {
      //   this.accountInfo.areaId = row.areaId;
      //   this.accountInfo.bankId = row.bankId;
      //   this.accountInfo.bankSiteId = row.bankSiteId;
      //   this.accountInfo.account = row.account;
      //   this.accountInfo.uploadFile = row.openCertificateFiles;
      //   this.accountInfo.upLoadAgreementFile = row.agreementFiles;
      //   this.accountInfo.id = row.id;
      this.accountInfo.account = row.account
      this.hasBankInfo(row.account)
      this.operationType = 'updateAccount'
      this.accountFormVisible = true
      //   this.getAcountInfo();
    },
    // 是否匹配银行推送的专户信息
    hasBankInfo(account) {
      this.axios({
        method: 'post',
        url: 'api-acc/account/expand/findBankAccountInfo',
        data: { account: account },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.isDisabled = false
          } else {
            this.isDisabled = true
            this.$message({
              message: res.data.errorMsg,
              type: 'warning',
            })
          }
        })
        .catch((error) => {})
    },
    // 关闭添加专户
    close_addDialog(val) {
      this.accountFormVisible = false
      // 保存成功则刷新信息
      if (val) {
        // this.getAcountInfo(); //获取专户信息
        this.$refs.pagination.getTableListData()
      }
    },
    // 获取专户信息
    // getAcountInfo() {
    //     this.axios({
    //         method: "post",
    //         url: "api-acc/companyaccount/expand/current",
    //     })
    //     .then((res) => {
    //         if (!res.data.hasError) {
    //             let data = res.data.result;
    //             this.accountInfo = res.data.result;
    //             // 子组件传值内容
    //             this.accountInfo.bankId = data.bankId;
    //             this.accountInfo.bankSiteId = data.bankSiteId;
    //             this.accountInfo.account = data.account;
    //             this.accountInfo.uploadFile = data.openCertificateFiles;
    //             this.accountInfo.upLoadAgreementFile = data.agreementFiles;
    //             this.accountInfo.id = data.id;
    //         } else {
    //             this.$message({
    //                 message: res.data.errorMsg,
    //                 type: "error",
    //             });
    //         }
    //     })
    //     .catch((error) => {
    //         this.$message.error("专户数据获取失败");
    //     });
    // },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  top: 212px;
}
</style>
